<script setup>
import { defineEmits, ref,onMounted } from 'vue'
import {saveApi, updateApi} from "../../../api/student.js";
import {allApi} from "../../../api/grade.js";
import {ElMessage} from "element-plus";
//定义事件--父级
const emit = defineEmits(['flush']);

const grades=ref([])

onMounted(async()=>{
  //请求接口  获取未开班的班级列表
  const r=await allApi({type:1})
  grades.value=r.data
})
// 表单数据
const formData = ref({
  id: 0,
  name: '',
  deptName: '',
  no: '',
  sex: ''
})
// 表单验证
const formRules = ref({
  name: [
    { required: true, message: '请输入名称', trigger: 'blur' },
    { min: 2, max: 20, message: '请输入2~20个字符', trigger: 'blur' }
  ]
})

// 提交验证
const formRef = ref()
const onSubmit = () => {
  formRef.value.validate(async (isSuccess) => {
    if (isSuccess) {
      let r;
      const isadd=formData.value.id==0||formData.value.id==undefined;
      if(isadd) {
        r=await saveApi(formData.value);
      }else {
        r=await updateApi(formData.value);
      }
      if(r.code==0){
        ElMessage({
          type: 'success',
          message: isadd?'新增学员成功':'修改学员成功'})
        state.value=false
        emit('flush')
      }else {
        ElMessage.error(r.data)
      }
    }
  })
}
// 对话框状态
const state = ref(false)

// 暴露
defineExpose({
  state,
  formData
})
</script>
<template>
  <el-dialog
    v-model="state"
    :title="`${formData.id>0? '修改' : '添加'} 学员信息`"
    width="600"
    :before-close="() => (state = false)"
  >
    <!-- 内容 -->
    <el-form
      v-if="state"
      size="large"
      label-width="80"
      :model="formData"
      :rules="formRules"
      ref="formRef"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="学员姓名" prop="">
            <el-input placeholder="请输入学员姓名" v-model="formData.name" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门名称" >
            <el-input placeholder="请输入部门名称" v-model="formData.deptName" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="学员工号">
            <el-input placeholder="请输入学员工号" v-model="formData.no" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="学员性别">
             <el-radio-group v-model="formData.sex" class="ml-4">
              <el-radio label="男" size="large">男</el-radio>
              <el-radio label="女" size="large">女</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
       <el-form-item label="所属班级">
            <el-select placeholder="请选择班级" v-model="formData.gid">
              <el-option v-for="g in grades" :key="g.id" :label="g.name" :value="g.id"/>
            </el-select>
          </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button type="default" @click="state = false">取消</el-button>
      </el-form-item>
    </el-form>
    <!-- 内容 -->
  </el-dialog>
</template>

<style>
.dvimg{
  width: 100px;
  height: 100px;
}
</style>
